package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/***
 * 注销用户：jdbc执行dml语句【delete】
 * 请输入用户名：tony
 * 返回结果：注销成功|注销失败
 * delete from userinfo where username=''
 *
 */
public class JDBCDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1,加载驱动：Class.forName("")
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2,创建连接对象：
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?" +
                        "characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai" +
                        "&rewriteBatchedStatements=true",
                "root",
                "123456"
        );
        Statement statement = connection.createStatement();
        //接受执行
        Scanner scanner = new Scanner(System.in);
        System.out.println("用户名：");
        String username = scanner.nextLine();
        String sql = "delete from userinfo where username = '" + username + "'";
        //i:表示运行条数
        int i = statement.executeUpdate(sql);
        if (i > 0) {
            System.out.println("注销用户成功");
        }else{
            System.out.println("注销用户失败");
        }
        connection.close();
    }
}
